﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace XFSale.Aux
{
    public partial class TabPage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtTabName.Text = Request["tabName"];
                if (txtTabName.Text == null)
                    txtTabName.Text = string.Empty;

                dsTab.SelectParameters.Clear();
                dsTab.SelectParameters.Add("tabName", "%" + txtTabName.Text.ToUpper() + "%");
            }
        }

        protected void gvTab_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void gvTab_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        }

        protected void gvTab_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            e.Cancel = true;

            GridViewRow row = gvTab.Rows[e.NewSelectedIndex];
            Response.Redirect("~/TabFld.aspx?tabName=" + row.Cells[0].Text);
        }

        protected void gvTab_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void gvTab_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = true;
            gvTab.Visible = false;
            pnlSearch.Visible = false;

            fvTab.Visible = true;
            fvTab.PageIndex = gvTab.PageIndex * gvTab.PageSize + e.NewEditIndex;
            fvTab.ChangeMode(FormViewMode.Edit);
        }

        protected void gvTab_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

        }

        protected void gvTab_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {

        }

        protected void gvTab_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            dsTabTemp.SelectCommand = "select count(*) from XmTabFld where tabName = :tabName";
            dsTabTemp.SelectParameters.Clear();
            dsTabTemp.SelectParameters.Add("tabName", Convert.ToString(e.Keys["tabName"]));
            IEnumerator em = dsTabTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
            em.MoveNext();
            DataRowView row = em.Current as DataRowView;
            int count = Convert.ToInt32(row[0]);
            if (count > 0)
            {
                e.Cancel = true;
                AlertError("数据表存在字段，请删除所有字段后再删除数据表");
                return;
            }
        }

        protected void gvTab_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {

        }

        protected void fvTab_PreRender(object sender, EventArgs e)
        {
            Button btnCtrl = null;
            TextBox txtCtrl = null;
            switch (fvTab.CurrentMode)
            {
                case FormViewMode.ReadOnly:
                    ChangeToShow();
                    break;

                case FormViewMode.Edit:
                    btnCtrl = fvTab.FindControl("btnInsert") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    txtCtrl = fvTab.FindControl("txtTabName") as TextBox;
                    txtCtrl.Enabled = false;

                    break;

                case FormViewMode.Insert:
                    btnCtrl = fvTab.FindControl("btnUpdate") as Button;
                    if (btnCtrl != null)
                    {
                        btnCtrl.Visible = false;
                    }

                    txtCtrl = fvTab.FindControl("txtTabName") as TextBox;
                    txtCtrl.Enabled = true;

                    break;
            }
        }

        protected void fvTab_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "Cancel".ToUpper())
            {
                ChangeToShow();
            }
        }

        protected void fvTab_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
        }

        protected void fvTab_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            ChangeToShow();
        }

        protected void fvTab_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            dsTabTemp.SelectCommand = "select count(*) from XmTab where upper(tabName) = upper(:tabName)";
            dsTabTemp.SelectParameters.Clear();
            dsTabTemp.SelectParameters.Add("tabName", Convert.ToString(e.Values["tabName"]));
            IEnumerator em = dsTabTemp.Select(new DataSourceSelectArguments()).GetEnumerator();
            em.MoveNext();
            DataRowView row = em.Current as DataRowView;
            int count = Convert.ToInt32(row[0]);
            if (count > 0)
            {
                e.Cancel = true;
                AlertError("数据表名重复");
                return;
            }
        }

        protected void fvTab_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            ChangeToShow();
        }

        protected void btnInsert_Click(object sender, ImageClickEventArgs e)
        {
            gvTab.Visible = false;
            pnlSearch.Visible = false;

            fvTab.Visible = true;
            fvTab.InsertItemTemplate = fvTab.EditItemTemplate;
            fvTab.ChangeMode(FormViewMode.Insert);
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            dsTab.SelectParameters.Clear();
            dsTab.SelectParameters.Add("tabName", "%" + txtTabName.Text.ToUpper() + "%");
            gvTab.DataBind();
        }

        private void ChangeToShow()
        {
            gvTab.Visible = true;
            pnlSearch.Visible = true;
            fvTab.Visible = false;
        }
    }
}
